<?php 
require_once 'lib/swift_required.php';

$email_address = $_POST["email_address"];

if ($email_address == '' or $email_address == null) {
	$email_address = $_GET["email_address"];
}

$type = $_POST["type"];

if ($type == '' or $type == null) {
	$type = $_GET["type"];
}
if ($type == '') {
	$type = 'apartment';
}


if (filter_var($email_address, FILTER_VALIDATE_EMAIL)) {

	$username = 'decisiones@xeliom.com';
	$password = 'nico.287';
	
	$filePath = 'planillas/'.$type.'.xlsx';
	$fileName = $type.'.xlsx';
	
	$transport = Swift_SmtpTransport::newInstance('mail.xeliom.com', 25)
	->setUsername($username)
	->setPassword($password);
	
	// Create the Mailer using your created Transport
	$mailer = Swift_Mailer::newInstance($transport);
	
	// Create a message
	$message = Swift_Message::newInstance()
		->setSubject('Toma de decisiones')
		->setFrom(array($username => 'Decisiones'))
		->setTo(array($email_address))
		->setBody('Gracias por participar!<br><br>Has solicitado: ' + $type, 'text/html')
		->attach(Swift_Attachment::fromPath($filePath)->setFilename($fileName))
	;
	
	try{
		$result = $mailer->send($message);
		$status = 'ok';
	}catch(Exception $e){
		$result = $e;
		$status = 'error';
		echo $e;
	}

} else{
	$result = 'No valid email address provided';
	$status = 'error';
}

$data = array('status' => $status, 'email' => $email_address, 'message' => $result);

$json = json_encode($data);

echo $json;
?>